<?php
class InfoAction extends CommonAction {
    public function index() {
        $info = M('Info');

        parent::search(array(
            'title'=>'信息标题',
            'user'=>'信息发布者',
        ));
        if(!isset($_GET['act'])) {
            parent::page($info->count());
            $allInfo = $info->field(array('id', 'title', 'user', 'content', 'stick', 'content'=>'fcontent'))
                            ->order('addtime DESC')
                            ->limit($this->limit)
                            ->select();
        } else if($_GET['act'] == 'search') {
            parent::page($info->where($this->map)->count());
            $allInfo = $info->field(array('id', 'title', 'user', 'content', 'stick', 'content'=>'fcontent'))
                            ->order('addtime DESC')
                            ->where($this->map)
                            ->limit($this->limit)
                            ->select();
        }

        $this->assign(array(
           'allinfo'=>$allInfo,
           'empty'=>'<tr><td colspan="6">没有任何数据</td></tr>'
        ));

        $this->display();
    }

    public function update() {
        $id = $this->_get('id');
        if(isset($id)) {
            $info = M('Info');
            if(!$info->where('id='.$id)->getField('id')) $this->error('不存在此信息!', 0);
            $one = $info->field(array('stick', 'stick_days'))->where('id='.$id)->find();

            switch($one['stick_days']) {
                case 86400:
                    $one['stick_days'] = '一天';
                    break;
                case 604800:
                    $one['stick_days'] = '一周';
                    break;
                case 2592000  :
                    $one['stick_days'] = '一月';
                    break;
                default:
                    $one['stick_days'] = '无';
                    break;
            }

            if(isset($_POST['send'])) {
                $level = $this->_post('level');
                $days = $this->_post('stick_days');
                switch($days) {
                    case 1:
                        $days = 3600*24;
                        break;
                    case 2:
                        $days = 3600*24*7;
                        break;
                    case 3:
                        $days = 3600*24*30;
                        break;
                    default:
                        $days = 0;
                        break;
                }
                $data['stick'] = $level;
                $data['stick_days'] = $days;
                if($level && $days) {
                    $data['stick_time'] = time();
                } else {
                    $data['stick_time'] = '';
                }
                $info->where('id='.$id)->save($data) ? $this->success('信息置顶成功!', __URL__) : $this->error('系统出错，置顶信息失败!', 0);
            }

            $this->assign('one', $one);
            $this->display();
        }
    }

    public function delete() {
        $id = $this->_get('id');
        if(isset($id)) {
            $info = M('Info');
            if(!$info->where('id='.$id)->getField('id')) $this->error('不存在此信息!', 0);
            $map['id'] = $id;
            $user = $info->where($map)->getField('user');
            $title = $info->where($map)->getField('title');
            $time = date('Y-m-d H:i:s');
            $message = M('Message');
            $data = array(
                'title'=>$user.',您好,你的信息已被删除',
                'content'=>'您好，您发布的信息[<strong style="color:#06f;">'.$title.'</strong>]已被删除，原因可能是<br />1、您的信息不符合发布规则(色情，暴力，政治)等<br />2、您的信息无价值意义<br />3、您的信息具有虚假成分<br />',
                'mfrom'=>'admin',
                'mto'=>$user,
                'is_read'=>0,
                'date'=>$time
            );
            $message->add($data);
            $info->where($map)->delete() ? $this->success('删除信息成功!', __URL__) : $this->error('系统出错，删除信息失败!', 0);
        }
    }

    public function mulDelete() {
        $ids = implode(',', $_POST['select']);
        if(!$ids) $this->error('请至少勾选一项!', 0);
        $info = M('Info');
        $map['id'] = array('in', $ids);
        $userAndTitle = $info->field(array('user', 'title'))->where($map)->select();
        $time = date('Y-m-d H:i:s');
        $message = M('Message');
        foreach ($userAndTitle as $key => $value) {
            $user = $value['user'];
            $title = $value['title'];
            $data = array(
                'title'=>$user.',您好,你的信息已被删除',
                'content'=>'您好，您发布的信息[<strong style="color:#06f;">'.$title.'</strong>]已被删除，原因可能是<br />1、您的信息不符合发布规则(色情，暴力，政治)等<br />2、您的信息无价值意义<br />3、您的信息具有虚假成分<br />',
                'mfrom'=>'admin',
                'mto'=>$user,
                'is_read'=>0,
                'date'=>$time
            );
            $message->add($data);
        }
        
        $info->where($map)->delete() ? $this->success('批量删除信息成功!', __URL__) : $this->error('系统出错，批量删除信息失败!', 0);
    }


}

?>